from rest_framework import serializers

from . import models
from django.conf import settings


class CourseCategorySerializer(serializers.ModelSerializer):
    """
    课程分类序列化其
    """

    class Meta:
        model = models.CourseCategory
        fields = ['id', 'name']


# class TeacherSerializer(serializers.ModelSerializer):
#     class Meta:
#         model = models.Teacher
#         # role_name 重写字段
#         # fields = ['id', 'name', 'role_name', 'title', 'signature', 'image', 'brief']
#         fields = ['id', 'name', 'title', 'signature', 'image', 'brief']


class CourseSerializer(serializers.ModelSerializer):
    """
    课程序列化器
    """
    # teacher = TeacherSerializer()  # 子序列化
    #
    teacher = serializers.SerializerMethodField()

    # section_list = serializers.SerializerMethodField()

    class Meta:
        model = models.Course
        fields = [
            'id',
            'name',
            'course_img',
            'brief',
            'attachment_path',  # 课件地址
            'pub_sections',  # 发布了多少课时
            'sections',  # 总共有多少
            'price',
            'students',
            'period',  # 建议学习周期

            'course_type_name',  # 课程类型 中文
            'level_name',  # 课程难度等级中文
            'status_name',  # 课程状态中文

            'teacher',  # 重写 返回老师的详情

            'section_list',  # 重写的字段，拿最多四个课时，超过4个就拿4个，不足4个，有几个拿几个
        ]

    def get_teacher(self, course_obj):
        data = {}
        # 'id', 'name', 'role_name', 'title', 'signature', 'image', 'brief'
        # print(course_obj.teacher.image.name)
        path = settings.BASE_URL + course_obj.teacher.image.name
        # print(course_obj.teacher.role_name, '>>>>')
        data['id'] = course_obj.teacher.id
        data['name'] = course_obj.teacher.name
        data['role_name'] = course_obj.teacher.get_role_display()
        data['title'] = course_obj.teacher.title
        data['signature'] = course_obj.teacher.signature
        data['image'] = path
        data['brief'] = course_obj.teacher.brief
        return data

    # def get_section_list(self, course_obj):
    #     """
    #     拿最多四个课时，超过4个就拿4个，不足4个，有几个拿几个
    #     :param course_obj:
    #     :return:
    #     """
    #     data = []
    #     return data


class CourseSectionSerializer(serializers.ModelSerializer):
    class Meta:
        model = models.CourseSection
        fields = ['id', 'name', 'section_type_name',
                  'section_link',
                  'duration',
                  'free_trail',
                  ]


class CourseChapterSerializer(serializers.ModelSerializer):
    '''
        拿出章节下所有课时，方案有三种
         -表模型中写
         -序列化类中写
         -方式三子序列化

    '''
    # 方式二
    # section_list=serializers.SerializerMethodField()
    # def get_section_list(self,obj):
    #     return []

    # 方式三：子序列化(在章节表中隐藏了通过related_name写的，coursesections这个字段，因为它可以通过章节对象.coursesections)
    coursesections = CourseSectionSerializer(many=True)  # 章节下的课时，是多条，使用子序列化，instance不用传，但是many=True必须传

    class Meta:
        model = models.CourseChapter
        fields = ['id', 'name', 'chapter', 'summary', 'coursesections']
